var base_url = 'http://ingepos.com/';
document.addEventListener("plusready", function() {
	/*
	根据窗口宽度   设置fontsize
	*/
	plus.key.addEventListener('backbutton', function() {
		if(!plus.webview.currentWebview().opener()) {
			return false;
		}
		mui.fire(plus.webview.currentWebview().opener(), "preshow");
		var res = plus.webview.hide(plus.webview.currentWebview(), "slide-out-right", 200);
		console.log(res);
		return false;
	}, false);
	$(".back").on('tap', function() {
		console.log('back');
		console.log(plus.webview.currentWebview().opener().id);
		if(plus.webview.currentWebview().opener().id == "HBuilder") {
			//			mui.openWindow('HBuilder');
			//			return false;
			//			console.log(plus.webview.currentWebview().opener().opener().id);
			//			console.log(plus.webview.getDisplayWebview().length);
		}
		mui.fire(plus.webview.currentWebview().opener(), "preshow");
		//		plus.webview.close(plus.webview.currentWebview(), "slide-out-right", 100);
		plus.webview.hide(plus.webview.currentWebview(), "slide-out-right", 100);
		//		mui.back();
		return false;
	});
	$('.gohome').on('click', function() {
		openNewWindows('home.html');
	});
	window.openNewWindows = function(id, data) { //list点击item后的事件
		console.log(id);
		var nwaiting = plus.nativeUI.showWaiting(); //显示原生等待框
		var iscreate = plus.webview.getWebviewById(id);
		if(!iscreate) {
			mui.preload({
				url: id,
				id: id,
				extras: {
					data
				},
			});
		} else {
			mui.fire(iscreate, "preshow");
		}
		nwaiting.close(); //新webview的载入完毕后关闭等待框
		mui.openWindow({
			url: id,
			id: id,
			extras: {
				data
			},
		}); //把新webview窗体显示出来，显示动画效果为速度200毫秒的右侧移入动画
	}
	window.is_login = function() {
		$.ajax({
			url: base_url + 'api/user/islogin',
			type: 'GET',
			async: true,
			success: function(data) {
				if(data.code == 0) {
					plus.storage.setItem('cookie', plus.navigator.getCookie(base_url));
					plus.storage.setItem('mobile', data.data.mobile);
					console.log(plus.storage.getItem('mobile'));
					return 1;
				} else {
					plus.storage.setItem('mobile', '0');
					return false;
				}
			},
			error: function(xhr, type, errorThrown) {
				//				mui.toast("网络错误");
				return false;
			}
		});
	}
	is_login();
	window.islogin = function() {
		is_login();
		if(plus.storage.setItem('mobile') == '0') {
			openNewWindows('login.html');
		}
	}
	//		init();
	$(window).init(function() {
		//				init();
	})

	function init() {
		//console.log("resize");
		var windowW = $(window).width();
		if(windowW > 640) { windowW = 640; }
		var fz = (windowW * 625) / 320;
		$("html").css("fontSize", fz + "%");
		$("#body").css("minHeight", $(window).height());
	}

	$('a').on('tap', function() {
		return false;
	});
	$('.address').on('click', function() {

		openNewWindows('address.html');
		return false;

	});
	
	$('#goaddress').on('click', function() {
		is_login();
		if(plus.storage.getItem('mobile') != 0) {
			openNewWindows('address.html');
			return false;
		} else {
			openNewWindows('login.html');
		}
	});
	
	$('#mycenter').on('click', function() {
		is_login();
		if(plus.storage.getItem('mobile') != 0) {
			openNewWindows('mycenter.html');
			return false;
		} else {
			openNewWindows('login.html');
		}
	});
}, false);